#include <STC89C5xRC.H>

sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;

unsigned char code LedChar[19]={    
	0xC0,       /*'0', 0*/
    0xF9,       /*'1', 1*/
    0xA4,       /*'2', 2*/
    0xB0,       /*'3', 3*/
    0x99,       /*'4', 4*/
    0x92,       /*'5', 5*/
    0x82,       /*'6', 6*/
    0xF8,       /*'7', 7*/
    0x80,       /*'8', 8*/
    0x90,       /*'9', 9*/
    0x88,       /*'A', 10*/
    0x83,       /*'B', 11*/
    0xC6,       /*'C', 12*/
    0xA1,       /*'D', 13*/
    0x86,       /*'E', 14*/
    0x8E,       /*'F', 15*/
    0xBF,       /*'-', 16*/
    0xFF,       /*' ', 17*/
    0x7F,       /*'.', 18*/
};

unsigned char LedBuff[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

unsigned char Flag = 0;
unsigned int cnt = 0;
unsigned long sec = 0;
unsigned char i = 0;

void main()
{
	unsigned int cnt = 0;
	unsigned long sec = 0;
	unsigned char i = 0;
	
	ENLED = 0;
	ADDR3 = 1;
	
	TMOD = 0x01;
	TH0 = 0xFC;
	TL0 = 0x66;
	TR0 = 1;
	ET0=1;
	EA=1;
	
	while(1)
	{
		if(Flag == 1)
		{
			Flag = 0;
			sec++;
			LedBuff[0] = LedChar[sec%10];
			LedBuff[1] = LedChar[sec/10%10];
			LedBuff[2] = LedChar[sec/100%10];
			LedBuff[3] = LedChar[sec/1000%10];
			LedBuff[4] = LedChar[sec/10000%10];
			LedBuff[5] = LedChar[sec/100000%10];
			if(sec>999999)
			{
				sec=0;
			}
		}
	}
	
}

void Timer0_Isr(void) interrupt 1
{
	TH0=0xFC;
	TL0=0x66;
	cnt++;
	if(cnt>=2)
	{
		cnt = 0;
		Flag = 1;
	}
	P0=0xFF;
	switch(i)
	{
		case 0:ADDR2=0;ADDR1=0;ADDR0=0;i++;P0=LedBuff[0];break;
		case 1:ADDR2=0;ADDR1=0;ADDR0=1;i++;P0=LedBuff[1];break;
		case 2:ADDR2=0;ADDR1=1;ADDR0=0;i++;P0=LedBuff[2];break;
		case 3:ADDR2=0;ADDR1=1;ADDR0=1;i++;P0=LedBuff[3];break;
		case 4:ADDR2=1;ADDR1=0;ADDR0=0;i++;P0=LedBuff[4];break;
		case 5:ADDR2=1;ADDR1=0;ADDR0=1;i=0;P0=LedBuff[5];break;
		default:break;
	}
}
